//201902104003邵潇漩
package bysj.service;

import bysj.dao.ApplicationForSupervisorDao;
import bysj.domain.ApplicationForSupervisor;
import bysj.domain.ApplicationForSupervisorEntry;
import lombok.extern.slf4j.Slf4j;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
public class ApplicationForSupervisorService {
    private static ApplicationForSupervisorDao applicationForSupervisorDao= ApplicationForSupervisorDao.getInstance();
    private static ApplicationForSupervisorService applicationForSupervisorService=new ApplicationForSupervisorService();
    private ApplicationForSupervisorService(){}
    public static ApplicationForSupervisorService getInstance(){
        return applicationForSupervisorService;
    }
    public boolean add(Collection<ApplicationForSupervisorEntry> applicationForSupervisorEntries,
                       ApplicationForSupervisor applicationForSupervisor) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false，开始事务
        connection.setAutoCommit(false);
        boolean added=false;
        int applicationForSupervisorId=0;
        try {
             applicationForSupervisorId=applicationForSupervisorDao.addApplicationForSupervisor(connection,applicationForSupervisor);
             added=applicationForSupervisorDao.addApplicationForSupervisorEntry(connection,applicationForSupervisorEntries,applicationForSupervisorId);
             connection.commit();
        }catch (SQLException e){
            connection.rollback();
            log.error(e.getMessage());
        }finally {
            //将自动提交设置为false，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }
}
